//
//  MainWindowController.swift
//  OnemeetingDemo
//
//  Created by zhangheng on 2019/11/18.
//  Copyright © 2019 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

class MainWindowController: NSWindowController {
    var winFrame:NSRect = NSRect.zero
    
    override init(window: NSWindow?) {
        super.init(window: window)
        self.contentViewController = self.viewController
        self.window = self.contentWindow//MainWindow.init(contentViewController: MainViewController())
        self.window?.center()
    }
    
    override func windowDidLoad() {
        super.windowDidLoad()
    }
    
    lazy var viewController: MainViewController = {
        let viewController = MainViewController()
        return viewController
    }()
    
    lazy var contentWindow: NSWindow = {
        let frame: CGRect = CGRect(x: 0, y: 0, width: kWindowWidth, height: kWindowHeight)
        let style: NSWindow.StyleMask = []
        let back: NSWindow.BackingStoreType = .buffered
        let window: NSWindow = NSWindow(contentRect: frame, styleMask: style, backing: back, defer: false)
        window.minSize = CGSize.init(width: kWindowWidth, height: kWindowHeight)
        window.title = ""
        
        window.contentView?.layer?.masksToBounds = true
        window.contentView?.layer?.cornerRadius = 5
        window.contentView?.wantsLayer = true
        window.backgroundColor = NSColor.clear
        
        window.hasShadow = true
        window.toolbar = NSToolbar.init(identifier: "main_contentWindow")
        window.titlebarAppearsTransparent = true
        window.titleVisibility = .hidden
        window.toolbar?.showsBaselineSeparator = false
        
        window.styleMask = [.fullSizeContentView,.titled,.closable,.miniaturizable]
        window.backingType = .buffered
        window.collectionBehavior = NSWindow.CollectionBehavior.fullScreenPrimary
        window.standardWindowButton(NSWindow.ButtonType.zoomButton)?.isHidden = true
        
        let closeBtn = window.standardWindowButton(NSWindow.ButtonType.closeButton)
        closeBtn?.action = #selector(windowClose)
        closeBtn?.target = self
        
        window.windowController = self
        return window
    }()
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func windowDidResize(_ notification: Notification) {
        if (self.window?.inLiveResize)!  {
            self.winFrame.origin.x -= ((window?.frame.size.width)! - self.winFrame.size.width)/2
            self.winFrame.size.width += ((window?.frame.size.width)! - winFrame.size.width)
            self.winFrame.origin.y = (window?.frame.origin.y)!
            self.winFrame.size.height = (window?.frame.height)!
            self.window?.setFrame(self.winFrame, display: true)
        }
    }
    
    func windowWillStartLiveResize(_ notification: Notification) {
        self.winFrame = (window?.frame)!
    }
    
    @objc func windowClose() {
        NSApp.terminate(nil)
    }
}
